package com.jd.controller;

import com.jd.pojo.Item;
import com.jd.pojo.ItemsImg;
import com.jd.pojo.ItemsParam;
import com.jd.pojo.ItemsSpec;
import com.jd.service.ItemService;
import com.jd.utils.JDJSONResult;
import com.jd.utils.PagedGridResult;
import com.jd.vo.CommentLevelCountsVO;
import com.jd.vo.ItemInfoVO;
import com.jd.vo.ShopcartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "商品接口",tags = {"商品信息展示的相关接口"})
@RestController
@RequestMapping("items")
public class ItemsController extends BaseController {

    @Autowired
    private ItemService itemService;



    @ApiOperation(value = "查询商品详情",notes = "查询商品详情",httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public JDJSONResult carousel(@ApiParam(name="itemId",value = "商品id",required = true)
                                 @PathVariable String itemId){
        if(StringUtils.isBlank(itemId)){
            return JDJSONResult.errorMsg(null);
        }
        //
        Item item=itemService.queryItemById(itemId);
        List<ItemsImg> itemImgList=itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecList =itemService.queryItemSpecList(itemId);
        ItemsParam itemsParam=itemService.queryItemParam(itemId);

        ItemInfoVO itemInfoVO=new ItemInfoVO();
        itemInfoVO.setItem(item);
        itemInfoVO.setItemImgList(itemImgList);
        itemInfoVO.setItemSpecList(itemsSpecList);
        itemInfoVO.setItemParams(itemsParam);
        return JDJSONResult.ok(itemInfoVO);
    }

    @ApiOperation(value = "查询商品评价等级",notes = "查询商品评价等级",httpMethod = "GET")
    @GetMapping("/commentLevel")
    public JDJSONResult commentLevel(@ApiParam(name="itemId",value = "商品id",required = true)
                                     @RequestParam String itemId){
        if(StringUtils.isBlank(itemId)){
            return JDJSONResult.errorMsg(null);
        }
        CommentLevelCountsVO countsVO= itemService.queryCommentCount(itemId);
        return JDJSONResult.ok(countsVO);
    }


    @ApiOperation(value = "查询商品评论",notes = "查询商品评论",httpMethod = "GET")
    @GetMapping("/comments")
    public JDJSONResult comments(   @ApiParam(name="itemId",value = "商品id",required = true)
                                    @RequestParam String itemId,
                                    @ApiParam(name="level",value = "评价等级",required = false)
                                    @RequestParam Integer level,
                                    @ApiParam(name="page",value = "查询下一页的第几页",required = false)
                                    @RequestParam Integer page,
                                    @ApiParam(name="pageSize",value = "分页的每一页显示的记录数",required = false)
                                    @RequestParam Integer pageSize){
        if(StringUtils.isBlank(itemId)){
            return JDJSONResult.errorMsg(null);
        }

        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=COMMENT_PAGE_SIZE;
        }
        PagedGridResult grid= itemService.queryPageComments(itemId,level,
                page,pageSize);
        return JDJSONResult.ok(grid);
    }


    @ApiOperation(value = "搜索商品列表",notes = "搜索商品列表",httpMethod = "GET")
    @GetMapping("/search")
    public JDJSONResult search(   @ApiParam(name="keywords",value = "关键字",required = true)
                                    @RequestParam String keywords,
                                    @ApiParam(name="sort",value = "排序字段",required = false)
                                    @RequestParam String sort,
                                    @ApiParam(name="page",value = "查询下一页的第几页",required = false)
                                    @RequestParam Integer page,
                                    @ApiParam(name="pageSize",value = "分页的每一页显示的记录数",required = false)
                                    @RequestParam Integer pageSize){
        if(StringUtils.isBlank(keywords)){
            return JDJSONResult.errorMsg(null);
        }

        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=PAGE_SIZE;
        }
        PagedGridResult grid= itemService.searchItems(keywords,sort,
                page,pageSize);
        return JDJSONResult.ok(grid);
    }



    @ApiOperation(value = "通过分离id搜索商品列表",notes = "通过分离id搜索商品列表",httpMethod = "GET")
    @GetMapping("/catItems")
    public JDJSONResult catItems(   @ApiParam(name="catId",value = "三级分类id",required = true)
                                  @RequestParam Integer catId,
                                  @ApiParam(name="sort",value = "排序字段",required = false)
                                  @RequestParam String sort,
                                  @ApiParam(name="page",value = "查询下一页的第几页",required = false)
                                  @RequestParam Integer page,
                                  @ApiParam(name="pageSize",value = "分页的每一页显示的记录数",required = false)
                                  @RequestParam Integer pageSize){
        if(catId==null){
            return JDJSONResult.errorMsg(null);
        }

        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=PAGE_SIZE;
        }
        PagedGridResult grid= itemService.searchItems(catId,sort,
                page,pageSize);
        return JDJSONResult.ok(grid);
    }


    /**
     * 用于用户长时间未登录网站，刷新购物车中的数据（主要是商品价格）类似京东、淘宝
     * @param itemSpecIds
     * @return
     */
    @ApiOperation(value = "通过分离id搜索商品列表",notes = "通过分离id搜索商品列表",httpMethod = "GET")
    @GetMapping("/refresh")
    public JDJSONResult refresh(@ApiParam(name="itemSpecIds",value = "拼接的规格ids",required = true,example = "1001,1003,1005")
                                 @RequestParam String  itemSpecIds){
        if(StringUtils.isBlank(itemSpecIds)){
            return JDJSONResult.ok();
        }
        List<ShopcartVO>  list= itemService.queryItemsBySpecIds(itemSpecIds);
        return JDJSONResult.ok(list);
    }



}
